//
//  LGDoorInfoViewController.m
//  ALS3.0
//
//  Created by guomianxiang on 2017/3/31.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "LGDoorInfoViewController.h"
#import "LGDCTableHead.h"
#import "ALSHeader.h"


#define k_sectionHeadViewHeight 35

@interface LGDoorInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray <NSArray <LGDCCellContent *>*>*cellInfoArr;

@end

@implementation LGDoorInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [MobClick beginLogPageView:UMPageName];

    k_registerNib(self.tableView)

    UIButton *backBtn = [ALSBarButton backButtonWithTitle:nil];
    [backBtn addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:(UIControlEventTouchUpInside)];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
    self.navigationItem.title = @"门详细信息";

}

- (void)viewDidDisappear:(BOOL)animated {
    [MobClick endLogPageView:UMPageName];

}

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    
    __weak typeof(self)weakSelf = self;
    
    UIPreviewActionStyle favorite;
    UIPreviewActionStyle dislike;
    
    switch (self.door.favorite) {
        case -1:
            favorite = UIPreviewActionStyleDestructive;
            dislike = UIPreviewActionStyleSelected;
            break;
        case 0:
            favorite = UIPreviewActionStyleDestructive;
            dislike = UIPreviewActionStyleDefault;
            break;
        case 1:
            favorite = UIPreviewActionStyleSelected;
            dislike = UIPreviewActionStyleDefault;
            break;
        default:
            favorite = 0;
            dislike = 0;
            break;
    }
    
    
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"常用" style:favorite handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
        
        if ([weakSelf.delegate respondsToSelector:@selector(doorInfoVCCommitActionType:withDoor:)]) {
            [self.delegate doorInfoVCCommitActionType:DoorInfoActionTypeSetFavorite withDoor:weakSelf.door];
        }
        
//        [DCMI setDoor:weakSelf.door Favorite:1 reply:^(NSIndexPath * _Nonnull changedIndexPath) {
//            
//        }];
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"不常用" style:dislike handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
        
        if ([weakSelf.delegate respondsToSelector:@selector(doorInfoVCCommitActionType:withDoor:)]) {
            [self.delegate doorInfoVCCommitActionType:DoorInfoActionTypeSetDislike withDoor:weakSelf.door];
        }

//        [DCMI setDoor:weakSelf.door Favorite:-1 reply:^(NSIndexPath * _Nonnull changedIndexPath) {
//            
//        }];
        
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"远程开门" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
        if ([weakSelf.delegate respondsToSelector:@selector(doorInfoVCCommitActionType:withDoor:)]) {
            [self.delegate doorInfoVCCommitActionType:DoorInfoActionTypeOpenDoor withDoor:weakSelf.door];
        }
        
    }];
    
    //    UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"选项组" style:UIPreviewActionStyleDefault actions:@[action1, action3]];
    
    return @[action1,action2,action3];
}


#pragma mark - UITableViewDataSource

k_tableViewDataSourceAndCellHeight(cellInfoArr, nil)

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return k_sectionHeadViewHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSArray <NSString *>*titles = @[@"门信息",@"控制器信息",@"相关门"];
    return [self tableViewSectionHeadViewWithTitle:titles[section]];
}

- (UIView *)tableViewSectionHeadViewWithTitle:(NSString *)title {
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = [UIColor clearColor];
    headView.frame = CGRectMake(0, 0, kScreen_W, k_sectionHeadViewHeight);
    
    
    UIView *highlightedView = [[UIView alloc]init];
    highlightedView.backgroundColor = kALSHightBlueColor;
    highlightedView.width = 4;
    highlightedView.height = headView.height * 0.4;
    highlightedView.x = 12;
    highlightedView.y = (headView.height - highlightedView.height ) / 2;
    highlightedView.layer.cornerRadius = 1;
    highlightedView.layer.masksToBounds = YES;
    [headView addSubview:highlightedView];

    
    UILabel *textLab = [[UILabel alloc]init];
    textLab.textColor = [UIColor whiteColor];
    textLab.font = [UIFont systemFontOfSize:15];
    textLab.text = title;
    [textLab sizeToFit];
    textLab.x = highlightedView.right + 5;
    textLab.y = (headView.height - textLab.height) / 2;
    [headView addSubview:textLab];
    return headView;
}
#pragma mark - load

- (void)setDoor:(LGDoor *)door {
    _door = door;
    self.cellInfoArr = [LGDCCellContent transfromCellContentArrWithDoor:door];
}


@end


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
